package org.blame.lotterysystem.controller;


import org.blame.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import org.blame.lotterysystem.common.exception.ControllerException;
import org.blame.lotterysystem.common.pojo.CommonResult;
import org.blame.lotterysystem.common.utils.JacksonUtil;
import org.blame.lotterysystem.controller.param.ShortMessageParam;
import org.blame.lotterysystem.controller.param.UserPasswordParam;
import org.blame.lotterysystem.controller.param.UserRegisterParam;
import org.blame.lotterysystem.controller.result.BaseUserInfoResult;
import org.blame.lotterysystem.controller.result.UserLoginResult;
import org.blame.lotterysystem.controller.result.UserRegisterResult;
import org.blame.lotterysystem.service.UserService;
import org.blame.lotterysystem.service.VerificationCodeService;
import org.blame.lotterysystem.service.dto.UserInfoDTO;
import org.blame.lotterysystem.service.dto.UserLoginDTO;
import org.blame.lotterysystem.service.dto.UserRegisterDTO;
import org.blame.lotterysystem.service.enums.UserIdentityEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
public class UserController {

    private static final Logger logger= LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private VerificationCodeService verificationCodeService;

    @RequestMapping("/register")
    public CommonResult<UserRegisterResult> register(@RequestBody @Validated UserRegisterParam param){
        logger.info("注册用户信息:{}", JacksonUtil.writeValueAsString(param));
        UserRegisterDTO userRegisterDTO=userService.register(param);
        UserRegisterResult resultRegister=convertUserRegisterDTOToUserRegisterResult(userRegisterDTO);
        return CommonResult.success(resultRegister);
    }

    @RequestMapping("/verification-code/send")
    public CommonResult<Boolean> sendVerificationCode(String phoneNumber){
        logger.info("sendVerificationCode:{}",JacksonUtil.writeValueAsString(phoneNumber));
        verificationCodeService.setVerificationCode(phoneNumber);
        return CommonResult.success(Boolean.TRUE);
    }

    @RequestMapping("/password/login")
    public CommonResult<UserLoginResult> UserPasswordLogin(@RequestBody @Validated UserPasswordParam userPasswordParam){
        logger.info("UserPasswordLogin  UserPasswordParam:{}",JacksonUtil.writeValueAsString(userPasswordParam));
        UserLoginDTO userLoginDTO=new UserLoginDTO();
        userLoginDTO=userService.login(userPasswordParam);
        UserLoginResult userLoginResult = convertUserLoginDTOToUserLoginResult(userLoginDTO);
        return CommonResult.success(userLoginResult);
    }


    @RequestMapping("/message/login")
    public CommonResult<UserLoginResult> ShortMessageLogin(@RequestBody @Validated ShortMessageParam shortMessageParam){
        logger.info("ShortMessageLogin  ShortMessageParam:{}",JacksonUtil.writeValueAsString(shortMessageParam));
        UserLoginDTO userLoginDTO=new UserLoginDTO();
        userService.login(shortMessageParam);
        userLoginDTO=userService.login(shortMessageParam);
        UserLoginResult userLoginResult = convertUserLoginDTOToUserLoginResult(userLoginDTO);
        return CommonResult.success(userLoginResult);
    }


    @RequestMapping("/base-user/find-list")
    public CommonResult<List<BaseUserInfoResult>> findUserInfo(String identity){
        logger.info("findUserInfo  identity:{}",JacksonUtil.writeValueAsString(identity));

        List<UserInfoDTO> UserInfoDTO=userService.findUserInfo(UserIdentityEnum.fromName(identity));
        return CommonResult.success(convertFindUserInfoDTOToBaseUserInfoResult(UserInfoDTO));
    }


    public List<BaseUserInfoResult> convertFindUserInfoDTOToBaseUserInfoResult(List<UserInfoDTO> userInfoDTOList){
        return userInfoDTOList.stream()
                .map(userInfoDTO -> {
                    BaseUserInfoResult baseUserInfoResult=new BaseUserInfoResult();
                    baseUserInfoResult.setUserId(userInfoDTO.getUserId());
                    baseUserInfoResult.setUserName(userInfoDTO.getUserName());
                    baseUserInfoResult.setIdentity(UserIdentityEnum.fromName(userInfoDTO.getIdentity()));
                    return baseUserInfoResult;
                }).collect(Collectors.toList());
    }

    private UserLoginResult convertUserLoginDTOToUserLoginResult(UserLoginDTO userLoginDTO) {
        if(userLoginDTO==null){
            throw new ControllerException(ControllerErrorCodeConstants.LOGIN_ERROR);
        }
        UserLoginResult userLoginResult=new UserLoginResult();
        userLoginResult.setToken(userLoginDTO.getToken());
        userLoginResult.setIdentity(userLoginDTO.getIdentity().name());
        return userLoginResult;
    }

    private UserRegisterResult convertUserRegisterDTOToUserRegisterResult(UserRegisterDTO userRegisterDTO) {
        if(userRegisterDTO==null){
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }

        UserRegisterResult result=new UserRegisterResult();
        result.setUserId(userRegisterDTO.getUserId());
        return result;
    }


}
